package com.bruce.pattern.structural.proxy.staticproxy;

import com.bruce.pattern.structural.proxy.IOrderService;
import com.bruce.pattern.structural.proxy.Order;
import com.bruce.pattern.structural.proxy.OrderServiceImpl;
import com.bruce.pattern.structural.proxy.db.DataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by bruce on 2018/11/8 22:14
 */
@Slf4j
public class OrderServiceStaticProxy implements IOrderService {

    private IOrderService iOrderService = new OrderServiceImpl();

    @Override
    public int saveOrder(Order order) {
        beforeMethod();

        int userId = order.getUserId();
        int dbRouter = userId % 2;

        log.info("静态代理分配到db:" + dbRouter);
        DataSourceContextHolder.setDBType("db"+String.valueOf(dbRouter));

        afterMethod();
        return iOrderService.saveOrder(order);
    }

    private void beforeMethod() {
        log.info("静态代理before");
    }

    private void afterMethod() {
        log.info("静态代理after");
    }


}
